/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-07-05     wcc       the first version
 */


#include "usr_ncov.h"

struct NCOV_DATA dataChinaNow = {0, 0, 0, 0,  "06-13 16:22"};
struct NCOV_DATA dataChinaAdd = {0, 0, 0, 0,  NULL};
struct TIME_DATA curTime ={"", "", "" };

int parseData(uint8_t *str)
{
     int ret =0;
     cJSON *ret_obj;
     cJSON *root_obj;
     rt_kprintf("**start to parse**\n");
     root_obj = cJSON_Parse((const char*)str); //创建JSON解析对象，返回JSON格式是否正确
     if (!root_obj) {
         ret = 0;
       rt_kprintf("**json format error**\n");
         return ret;

     } else {

       rt_kprintf("**json format OK**\n");
       ret_obj = cJSON_GetObjectItem(root_obj, "ret");
       //if(cJSON_IsNumber(ret_obj))
       //{
        // ret = 1;
       //  ret = ret_obj->valueint;
       //}

       char *data_str = cJSON_GetObjectItem(root_obj, "data")->valuestring;
       cJSON *data_obj = cJSON_Parse(data_str);
       if (!data_obj) {
         rt_kprintf("**data json err*\n");
         ret = 0;
       } else {
        rt_kprintf("**data json ok**\n");
         char *lastUpdateTime =
             cJSON_GetObjectItem(data_obj, "lastUpdateTime")->valuestring;
         rt_kprintf("**data json ok %s **\n", lastUpdateTime);
         rt_memcpy(dataChinaNow.updateTime, lastUpdateTime, 20);
         cJSON *chinaTotal_obj = cJSON_GetObjectItem(data_obj, "chinaTotal");
         dataChinaNow.currentConfirmedCount = cJSON_GetObjectItem(chinaTotal_obj, "confirm")->valueint;
         dataChinaNow.currentHeal= cJSON_GetObjectItem(chinaTotal_obj, "heal")->valueint;
         dataChinaNow.currentDead = cJSON_GetObjectItem(chinaTotal_obj, "dead")->valueint;
         dataChinaNow.currentImportCase =
             cJSON_GetObjectItem(chinaTotal_obj, "importedCase")->valueint;
         //int chinaTotal_suspect =
         //    cJSON_GetObjectItem(chinaTotal_obj, "suspect")->valueint;
         //int chinaTotal_nowSevere =
         //    cJSON_GetObjectItem(chinaTotal_obj, "nowSevere")->valueint;
         cJSON *chinaAdd_obj = cJSON_GetObjectItem(data_obj, "chinaAdd");
         dataChinaAdd.currentConfirmedCount =
         cJSON_GetObjectItem(chinaAdd_obj, "confirm")->valueint;
         dataChinaAdd.currentHeal = cJSON_GetObjectItem(chinaAdd_obj, "heal")->valueint;
         dataChinaAdd.currentDead = cJSON_GetObjectItem(chinaAdd_obj, "dead")->valueint;

         dataChinaAdd.currentImportCase =
         cJSON_GetObjectItem(chinaAdd_obj, "importedCase")->valueint;
         rt_kprintf("**********china ncov data**********\n");

         rt_kprintf("%-23s: %8d, %-23s: %8d\n", "confirmedCount", dataChinaNow.currentConfirmedCount, "importedCase", dataChinaNow.currentImportCase);

         rt_kprintf("%-23s: %8d, %-23s: %8d\n", "curedCount", dataChinaAdd.currentConfirmedCount, "AddedimportedCase", dataChinaAdd.currentImportCase);

       }
       //        cJSON_Delete(ret_obj);
       //        cJSON_Delete(data_obj);
       cJSON_Delete(root_obj); //释放内存
       return ret;
      //updateTime = (time_t)

     }
}

int get_NCOV_Data(void)
{
    char *uri = RT_NULL;
    struct webclient_session* session = RT_NULL;
    uint8_t *buffer = RT_NULL;
    int index, ret = 0;
    int bytes_read, resp_status;
    int content_length = -1;
    int buffer_size = 1600;
    uri = web_strdup(API_NCOV);
    rt_kprintf("start get api: %s\r\n", API_NCOV);
    if(uri != RT_NULL)
    {
        rt_kprintf("start web_malloc \r\n");
        buffer = (unsigned char *) web_malloc(buffer_size);
        if (buffer == RT_NULL)
        {
            rt_kprintf("no memory for receive buffer.\n");
            ret = -RT_ENOMEM;
            goto __exit;
        }

        /* create webclient session and set header response size */
        session = webclient_session_create(buffer_size);
        if (session == RT_NULL)
        {
            rt_kprintf("start webclient_session_create \r\n");
            ret = -RT_ENOMEM;
            goto __exit;
        }
        rt_kprintf("send GET request by default header \r\n");
        /* send GET request by default header */
        if ((resp_status = webclient_get(session, uri)) != 200)
        {
            rt_kprintf("webclient GET request failed, response(%d) error.\n", resp_status);
            ret = -RT_ERROR;
            goto __exit;
        }

        rt_kprintf("webclient get response data: \n");

        content_length = webclient_content_length_get(session);
        if (content_length < 0)
        {
            rt_kprintf("webclient GET request type is chunked.\n");

            do
            {
                bytes_read = webclient_read(session, buffer, buffer_size);
                if (bytes_read <= 0)
                    break;

                for (index = 0; index < bytes_read; index++)
                {
                    rt_kprintf("%c", buffer[index]);
                }
            } while (1);

            rt_kprintf("\n");
        }
        else
        {
            /* 读取服务器响应的数据 */
            bytes_read = webclient_read(session, buffer, content_length);
            rt_kprintf("data length:%d\n", bytes_read);

            buffer[bytes_read] = '\0';
            rt_kprintf("parse data\r\n");
            parseData(buffer);
            rt_kprintf("\n");
        }

        __exit:
        if (session)
            webclient_close(session);

        if (buffer)
            web_free(buffer);
    }
    else
        rt_kprintf("api error: %s\n", API_NCOV);

    return ret;
}
MSH_CMD_EXPORT(get_NCOV_Data, get api weather);
